﻿#region Imports
using System;
using System.Collections.ObjectModel;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using CommonUtilities.Enums;
using CommonUtilities.Extensions;
using CommonUtilities.Randomizer;
using CommonUtilities.Util;
using OneStopProgrammingMatrix.Models;
using OneStopProgrammingMatrix.Properties;
#endregion

namespace OneStopProgrammingMatrix.Cache
{
    public static class DataCache
    {
        public const string sMyApps = "My _Apps";

        private static ObservableCollection<MatrixItem> dbMatrixItems;
        public static ObservableCollection<MatrixItem> DBMatrixItems
        {
            get
            {
                return dbMatrixItems._ForEach(itm =>
                    {
                        if (itm.MatrixGroup == Resources.Default_MatrixGroup) itm.MatrixGroup = "";
                    }).ToObservableCollection();
            }
            set { dbMatrixItems = value; }
        }

        public static string DBpath
        {
            get
            {
                return ConfigurationManager.AppSettings["DBPath"]
                    .Replace("~", Assembly.GetExecutingAssembly().Location.GetDirectoryNameFull())
                    .AppendPath(ConfigurationManager.AppSettings["MatrixDB"]);
            }
        }

        public static void WriteDefaultMatrix(ObservableCollection<MatrixItem> allLinks)
        {
            allLinks.Add(new MatrixItem
            {
                Name = "VS 2010",
                MatrixGroup = "Programming",
                Path = @"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe"
            });
            allLinks.Add(new MatrixItem
            {
                Name = "Beyond Compare",
                MatrixGroup = "Programming",
                Path = @"C:\Program Files (x86)\Beyond Compare 3\BCompare.exe"
            });
            allLinks.Add(new MatrixItem
            {
                Name = "Notepad++",
                MatrixGroup = "Editor",
                Path = @"C:\Program Files (x86)\Notepad++\notepad++.exe",
                Description = "A free (GNU) source code editor"
            });
            allLinks.Add(new MatrixItem
            {
                Name = "Bulk Renamer",
                MatrixGroup = sMyApps,
                Path = @"C:\Documents and Settings\rhythm divine\Documents\Visual Studio 2010\Projects\BulkRenamer\OUTPUT\Debug\BulkRenamer.exe",
                Description = "Renames files in bulk"
            });

            // Default template + testing for Un-grouped control
            allLinks.Add(new MatrixItem { Name = "__" + TextGenerator.Generate(10), Path = @"" });

            WriteMatrix(allLinks);
        }

        public static void WriteMatrix(ObservableCollection<MatrixItem> allLinks, string path = null)
        {
            if (!File.Exists(path)) path = DBpath;

            string sXmlData = allLinks.SerializeToFormattedData<ObservableCollection<MatrixItem>>(eSerializationFormat.Xml);
            File.WriteAllText(path, sXmlData);
        }

        public static ObservableCollection<MatrixItem> ReadMatrix(string path = null)
        {
            if (!File.Exists(path)) path = DBpath;

            var items = File.ReadAllText(path).DeSerializeFromFormattedData<ObservableCollection<MatrixItem>>(eSerializationFormat.Xml);
            bool needWriteBack = items._Where(lnk => lnk.PKey.Coalesce(Guid.Empty) == Guid.Empty).Any();

            items._Where(lnk => lnk.PKey.Coalesce(Guid.Empty) == Guid.Empty)._ForEach(lnk => lnk.PKey = Guid.NewGuid());
            if (needWriteBack) WriteMatrix(items, path);

            return items;
        }
    }
}
